﻿using System;
using System.Net;
using System.Runtime.Serialization;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;

namespace OverlakeApp.ViewModels
{
#if PC
    using AppService = OverlakeWSTester.AppService;
#endif

    /// <summary>
    /// A helper class that manages login and username/password storage.
    /// </summary>
    public sealed class LoginService : ViewModelBase
    {
        private static readonly AppService.OverlakeAppService_1_0SoapClient cl = new AppService.OverlakeAppService_1_0SoapClient();

        /// <summary>
        /// The user's username.
        /// </summary>
        public string Username
        {
            get
            {
                return Settings.GetValueOrDefault<string>("username", null);
            }
            set
            {
                if (this.Username != value)
                {
                    Settings.AddOrUpdateValue("username", value);
                    Settings.Save();
                    this.OnPropertyChanged("Username");
                }
            }
        }

        /// <summary>
        /// The user's password.
        /// </summary>
        public string Password
        {
            get
            {
                return Settings.GetValueOrDefault<string>("password", null);
            }
            set
            {
                Settings.AddOrUpdateValue("password", value);
                Settings.Save();
                this.OnPropertyChanged("Password");
            }
        }

        /// <summary>
        /// Initializes the login service.
        /// </summary>
        public LoginService()
        {
            cl.LoginCompleted += new EventHandler<AppService.LoginCompletedEventArgs>(cl_LoginCompleted);
            //cl.Endpoint.Behaviors.Add(new HttpUserAgentEndpointBehavior());
        }

        void cl_LoginCompleted(object sender, AppService.LoginCompletedEventArgs e)
        {
            this.UserInfo = e.Result;

            if (this.LoginAsyncCompleted != null)
                this.LoginAsyncCompleted(this, e);
        }

        /// <summary>
        /// Fired when an asyncronous login operation completes.
        /// </summary>
        public event EventHandler<AppService.LoginCompletedEventArgs> LoginAsyncCompleted;

        private AppService.UserInfo _UserInfo;
        /// <summary>
        /// The current information about the user.
        /// </summary>
        public AppService.UserInfo UserInfo
        {
            get
            {
                return this._UserInfo;
            }
            set
            {
                if (this._UserInfo != value)
                {
                    this._UserInfo = value;
                    this.OnPropertyChanged("UserInfo");
                }
            }
        }

        /// <summary>
        /// Asyncronously attempts to log into the remote web service using the current Username and Password properties.
        /// </summary>
        public void LoginAsync()
        {
            cl.LoginAsync(this.Username, this.Password);
        }
    }
}
